home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Development / 3DMF parser / 0.9 version / MFEXAMPL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-21  |  4.5 KB  |  151 lines  |  [TEXT/MPS ]

  1. /*==============================================================================
  2.  *
  3.  *    File:        MPWRefExample.c
  4.  *
  5.  *    Function:    QuickDraw 3D Metafile Read/Write Sample Code
  6.  *                This code shows how to resolve internal and external references
  7.  *                which are read from a QuickDraw 3D metafile.
  8.  *
  9.  *    Author(s):    Rick Wong (RWW).
  10.  *
  11.  *    Copyright:    (c) 1995 by Apple Computer, Inc., all rights reserved.
  12.  *
  13.  *    Change History (most recent first):
  14.  *        F3M_RWW    Added this header.
  15.  *==============================================================================
  16.  */
  17.  
  18. #include <stdio.h>                /* printf */
  19.  
  20. #include "MF3D.H"                /* MF3D API            */
  21. #include "MFERRORS.H"            /* MF3D Error codes    */
  22.  
  23. int
  24. main(int argc, char *argv[])
  25. {
  26.     MF3D_FilePtr        metafilePtr;        /* MF3D internal file pointer    */
  27.     MF3DVoidObjPtr        object;                /* object returned by Read        */
  28.     int                    objCount;            /* count the objects as we read    */
  29.     MF3DBoolean            nextObjIsRoot;        /* next obj read will be root    */
  30.     MF3DErr                result;                /* our result code                */
  31.  
  32.     if (argc != 2)
  33.     {    printf("# Usage: %s fileName", argv[0]);
  34.         return -1;
  35.     }
  36.  
  37.     result = kMF3DNoErr;
  38.  
  39.     if (result == kMF3DNoErr)
  40.     {    /* Open the metafile */
  41.         result = MF3DOpenInputStdCFile(argv[1], &metafilePtr);
  42.         if (result != kMF3DNoErr)
  43.             printf("# Open Input failed. Error %ld.\n", (long)result);
  44.     }
  45.  
  46.     objCount = 0;
  47.     nextObjIsRoot = kMF3DBooleanFalse;
  48.  
  49.     while (result == kMF3DNoErr)
  50.     {    /* Read an object from the metafile */
  51.         result = MF3DReadAnObject(metafilePtr, &object);
  52.  
  53.         ++objCount;
  54.  
  55.         if (result != kMF3DNoErr)
  56.         {    if (result != kMF3DNoMoreObjects)
  57.             {    printf("# Read failed for object #%d. Error %ld.\n", objCount,
  58.                         (long)result);
  59.             }
  60.         }
  61.         else
  62.         {    printf("# Read succeeded for object #%d (%.4s).\n", objCount,
  63.                     &object->objectType);
  64.         }
  65.  
  66.         if (result == kMF3DNoErr && object->objectType == kMF3DObjReference)
  67.         {    /* If we have just read a Reference object, resolve it.
  68.              * There are two types of references: local and external.
  69.              * An external reference will be the root object of a container
  70.              * and will have a subobject specifying the path
  71.              * to an external file.
  72.              */
  73.             MF3DVoidObjPtr    extStorageObj;
  74.  
  75.             extStorageObj = NULL;
  76.             if (nextObjIsRoot == kMF3DBooleanTrue)
  77.             {    /* We are in a container which must mean we have an
  78.                  * external reference.
  79.                  */
  80.                 result = MF3DReadAnObject(metafilePtr, &extStorageObj);
  81.  
  82.                 ++objCount;
  83.                 if (result != kMF3DNoErr)
  84.                 {    printf("# Read failed for object #%d. Error %ld.\n",
  85.                             objCount, (long)result);
  86.                 }
  87.                 else
  88.                 {    printf("# Read succeeded for object #%d (%.4s).\n",
  89.                             objCount, &extStorageObj->objectType);
  90.                 }
  91.  
  92.                 /* Because this sample code is written on a Macintosh,
  93.                  * we expect a Macintosh-style path here.
  94.                  */
  95.                 if (result == kMF3DNoErr &&
  96.                         extStorageObj->objectType != kMF3DObjMacintoshPath)
  97.                 {    printf("# Resolve external reference failed because "
  98.                             "external storage is not a Macintosh path.\n");
  99.                     result = -2;
  100.                 }
  101.             }
  102.  
  103.             /* Resolve the reference. This causes metafilePtr to point
  104.              * to the referenced object, so that the next call to
  105.              * MF3DReadAnObject will read the object which is being
  106.              * referenced. MF3DReadAnObject will then restore the correct
  107.              * file position after the referenced object has been read.
  108.              *
  109.              * If the reference is local, then extStorageObj is NULL.
  110.              * If the reference is external, then the file specified by
  111.              * extStorageObj will be opened automatically (and closed
  112.              * automatically after the reference has been resolved).
  113.              */
  114.             if (result == kMF3DNoErr)
  115.             {    result = MF3DResolveReference(metafilePtr,
  116.                         (MF3DReferenceObjPtr) object,
  117.                         (MF3DStorageObjPtr) extStorageObj);
  118.             }
  119.  
  120.             /* Dispose the external storage object, if there is one.
  121.              * The Reference object will be disposed later.
  122.              */
  123.             MF3DDisposeObject(extStorageObj);
  124.         }
  125.  
  126.         /* If the object we just read is a BeginContainer object, the next
  127.          * object read will be the root object of the container.
  128.          */
  129.         if (result == kMF3DNoErr && object->objectType == kMF3DObjContainer)
  130.             nextObjIsRoot = kMF3DBooleanTrue;
  131.         else
  132.             nextObjIsRoot = kMF3DBooleanFalse;
  133.  
  134.         /* Dispose the object (a real program would copy data first) */
  135.         if (result == kMF3DNoErr)
  136.         {    result = MF3DDisposeObject(object);
  137.             if (result != kMF3DNoErr)
  138.             {    printf("# Dispose failed for object #%d (%.4s). Error %ld.\n",
  139.                         objCount, &object->objectType, (long)result);
  140.             }
  141.         }
  142.     }
  143.  
  144.     /* Close does nothing if metafilePtr is NULL */
  145.     result = MF3DClose(metafilePtr);
  146.     if (result != kMF3DNoErr)
  147.         printf("# Close input failed. Error %ld.\n", (long)result);
  148.  
  149.     return result;
  150. }
  151.